package com.twq.filter;

import com.twq.entity.Admin;
import com.twq.entity.User;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = {"/main.html"},filterName = "adminFilter")

public class AdminFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request1, ServletResponse response1, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) request1;
        HttpServletResponse response = (HttpServletResponse) response1;
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null){
            response.sendRedirect("login.html");
        }else {
            chain.doFilter(request1,response1);
        }

    }

    @Override
    public void destroy() {

    }
}
